﻿Imports System.Activities
Imports System.Data.SqlClient
Imports System.Activities.Presentation.Metadata
Imports System.Activities.Presentation
Imports System.Drawing
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing

Public Class CreateDBConnectActivity
    Inherits CodeActivity
    Implements IRegisterMetadata

#Region "プロパティ定義"
    Public Property UserID As InArgument(Of String)
    Public Property Password As InArgument(Of String)

    Public Property DataSource As InArgument(Of String)
    Public Property FailoverPartner As InArgument(Of String)
    Public Property InitialCatalog As InArgument(Of String)

    Public Property ApplicationName As InArgument(Of String)
    Public Property WorkstationID As InArgument(Of String)
    Public Property ConnectTimeout As InArgument(Of Integer)
    Public Property CurrentLanguage As InArgument(Of String)
    Public Property NetworkLibrary As InArgument(Of String)
    Public Property AttachDBFilename As InArgument(Of String)

    Public Property AsynchronousProcessing As InArgument(Of Boolean)
    Public Property ContextConnection As InArgument(Of Boolean)
    Public Property Encrypt As InArgument(Of Boolean)
    Public Property IsReadOnly As InArgument(Of Boolean)
    Public Property LoadBalanceTimeout As InArgument(Of Integer)
    Public Property MaxPoolSize As InArgument(Of Integer)
    Public Property MinPoolSize As InArgument(Of Integer)
    Public Property MultipleActiveResultSets As InArgument(Of Boolean)
    Public Property PacketSize As InArgument(Of Integer)
    Public Property PersistSecurityInfo As InArgument(Of Boolean)
    Public Property Pooling As InArgument(Of Boolean)
    Public Property Replication As InArgument(Of Boolean)
    Public Property TrustServerCertificate As InArgument(Of Boolean)
    Public Property TypeSystemVersion As InArgument(Of String)
    Public Property UserInstance As InArgument(Of Boolean)

    Public Property ConnectStrings As OutArgument(Of String)
#End Region

    Public Sub New()
        Me.DisplayName = My.Resources.CreateDBConnectActivityDisplayName
    End Sub

    Protected Overrides Sub Execute(context As System.Activities.CodeActivityContext)
        Dim resultBuilder As New SqlConnectionStringBuilder

        With resultBuilder
            .UserID = context.GetValue(Me.UserID)
            .Password = context.GetValue(Me.Password)
            If .UserID.Trim = "" AndAlso .Password.Trim = "" Then
                .IntegratedSecurity = True
            Else
                .IntegratedSecurity = False
            End If

            .DataSource = context.GetValue(Me.DataSource)
            .FailoverPartner = context.GetValue(Me.FailoverPartner)
            .InitialCatalog = context.GetValue(Me.InitialCatalog)

            .ApplicationName = context.GetValue(Me.ApplicationName)
            .WorkstationID = context.GetValue(Me.WorkstationID)
            .ConnectTimeout = context.GetValue(Me.ConnectTimeout)
            .CurrentLanguage = context.GetValue(Me.CurrentLanguage)
            .NetworkLibrary = context.GetValue(Me.NetworkLibrary)
            .AttachDBFilename = context.GetValue(Me.AttachDBFilename)

            .AsynchronousProcessing = context.GetValue(Me.AsynchronousProcessing)
            .ContextConnection = context.GetValue(Me.ContextConnection)
            .Encrypt = context.GetValue(Me.Encrypt)
            .LoadBalanceTimeout = context.GetValue(Me.LoadBalanceTimeout)
            .MaxPoolSize = context.GetValue(Me.MaxPoolSize)
            .MinPoolSize = context.GetValue(Me.MinPoolSize)
            .MultipleActiveResultSets = context.GetValue(Me.MultipleActiveResultSets)
            .PacketSize = context.GetValue(Me.PacketSize)
            .PersistSecurityInfo = context.GetValue(Me.PersistSecurityInfo)
            .Pooling = context.GetValue(Me.Pooling)
            .Replication = context.GetValue(Me.Replication)
            .TrustServerCertificate = context.GetValue(Me.TrustServerCertificate)
            .TypeSystemVersion = context.GetValue(Me.TypeSystemVersion)
            .UserInstance = context.GetValue(Me.UserInstance)
        End With

        context.SetValue(Me.ConnectStrings, resultBuilder.ConnectionString)
    End Sub


    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'CreateDBConnectActivity Activity
        build.AddCustomAttributes(GetType(CreateDBConnectActivity),
                                              New ToolboxBitmapAttribute(GetType(CreateDBConnectActivity), "CreateConnectionStrings.png"))

        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "Encrypt",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "AsynchronousProcessing",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "ContextConnection",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "MultipleActiveResultSets",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "PersistSecurityInfo",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "Pooling",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "Replication",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "TrustServerCertificate",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))
        build.AddCustomAttributes(GetType(CreateDBConnectActivity), "UserInstance",
                                  New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class

<ToolboxBitmap("CreateConnectionStrings.png")>
Public Class CreateDBConnectActivityDesigner
    Inherits ActivityDesigner
End Class